package com.example.beikeyigou.goods;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beikeyigou.R;
import com.example.beikeyigou.untils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

public class GoodsTypeAdapter extends BaseAdapter {
    Context context;
    List<GoodsBean.DataBean> data;

    public GoodsTypeAdapter(Context context, List<GoodsBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_goodstype,parent,false);
            vh = new VH(convertView);
            convertView.setTag(vh);
        }else {
            vh = (VH) convertView.getTag();
        }

        GoodsBean.DataBean dataBean = data.get(position);
        vh.titleTv.setText(dataBean.getName());
        vh.priceTv.setText(String.valueOf(dataBean.getPrice()) + "元");
        vh.desTv.setText(dataBean.getInfo());

        String photo = dataBean.getPhoto();
        if (!TextUtils.isEmpty(photo)){
            String phonour1 = URLUtils.PUBLIC_URL+photo;
            Picasso .with(context).load(phonour1).into(vh.iv);
        }
        return  convertView;
    }
}

class VH{
    TextView titleTv,priceTv, desTv;
    ImageView iv;
    public  VH(View view){
        titleTv = view.findViewById(R.id.item_goodstype_tv_title);
        priceTv = view.findViewById(R.id.item_goodstype_tv_price);
        desTv = view.findViewById(R.id.item_goodstype_tv_des);
        iv = view.findViewById(R.id.item_goodstype_iv);
    }


}
